package org.munoz.shared;

import java.util.Date;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;

@Entity
public class Match {
	@Id
	private Long id;
	private Key<PlayerOnline> player;
	private String playerEmail;
	private Key<PlayerOnline> opponent;
	private String opponentEmail;
	private String state;
	Long matchStartDate;
	
	public Match() {
	}
	// TODO: CHange name of variables. I am not using their emails anymore but their fb id.
	public Match(Key<PlayerOnline> player, String playerEmail, Key<PlayerOnline> opponent, String opponentEmail,
			String state) {
		this.player = player;
		this.playerEmail = playerEmail;
		this.opponent = opponent;
		this.opponentEmail = opponentEmail;
		this.state = state;
		matchStartDate = new Date().getTime();
	}
	/**
	 * @return match starting date
	 */
	public Long getMatchStartDate() {
		return matchStartDate;
	}

	/**
	 * Sets match starting date
	 * @param matchStartDate
	 */
	public void setMatchStartDate(Long matchStartDate) {
		this.matchStartDate = matchStartDate;
	}

	/**
	 * @return match id
	 */
	public Long getId() {
		return id;
	}
	
	/** Sets match id
	 * @param id
	 */
	public void setId(Long id) {
		this.id = id;
	}
	
	/**
	 * @return returns player 1 
	 */
	public Key<PlayerOnline> getPlayer() {
		return player;
	}
	
	/** Sets player 1
	 * @param player
	 */
	public void setPlayer(Key<PlayerOnline> player) {
		this.player = player;
	}
	
	/**
	 * @param p
	 * @return Player 2 of the match
	 */
	public Key<PlayerOnline> getOpponent(Key<PlayerOnline> p) {
		if(p.equals(player)){
			System.out.println("Match " + id + ": returned opponent " + opponent);
			return opponent;
		}
		else {
			System.out.println("Match " + id + ": returned player " + player);
			return player;
		}
//		else if(player.equals(opponent)){
//			return player;
//		}
//		else{
//			return null;
//		}
	}
	
	/**
	 * Sets the opponent of the game
	 * @param opponent
	 */
	public void setOpponent(Key<PlayerOnline> opponent) {
		this.opponent = opponent;
	}
	
	/**
	 * @return state of the game saved in match
	 */
	public String getState() {
		return state;
	}
	
	/**
	 * Saves state in match 
	 * @param state
	 */
	public void setState(String state) {
		this.state = state;
	}
	
	/**
	 * @param playerKey
	 * @return whether it is player
	 */
	public boolean isPlayer1(Key<PlayerOnline> playerKey) {
		return player.equals(playerKey);
	}
	
	/**
	 * @param playerKey
	 * @return whether it is opponent
	 */
	public boolean isPlayer2(Key<PlayerOnline> playerKey) {
		return player.equals(opponent);
	}

	/**
	 * @return whether is the turn of the player
	 */
	public boolean isPlayer1Turn() {
		if(state.equals("")){
			return true;
		}
		if(state.charAt(0) == '0'){
			return true;
		}
		return false;
	}
	
	/**
	 * Remover the player 
	 * @param player
	 */
	public void removePlayer(Key<PlayerOnline> player) {
	    if (this.player.equals(player)) {
	    	this.player = null;
	    }
	    else if (opponent.equals(player)) {
	    	opponent = null;
	    }
	}
	
}